package com.course.utils.web;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baselib.android.BaseApp;
import com.baselib.android.cach.PreferceKey;
import com.baselib.android.cach.PreferceManager;
import com.baselib.android.utils.Logger;
import com.course.model.req.BaseReq;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtils {


    public static void post(String url, @NonNull String json, Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request()
                                .newBuilder()
                                .addHeader("Content-Type", "application/json; charset=UTF-8")
                                .addHeader("X_TOKEN", PreferceManager.getInsance().getValueBYkey(BaseApp.getInstance().getApplicationContext(), PreferceKey.TOKEN))
                                .build();
                        return chain.proceed(request);
                    }
                }).retryOnConnectionFailure(false).build();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                json);
        Request request = new Request.Builder()
                .post(body)
                .url(url).
                build();
        client.newCall(request).enqueue(callback);
    }

}
